import { Component, OnInit } from '@angular/core';
import { MerchantwineService } from '../../service/merchantwine.service';
import { MerchantinfoService } from '../../service/merchantinfo.service';
import { NzMessageService } from 'ng-zorro-antd';
import { CommonService } from 'src/app/service/common.service';
import { ComponentBasic } from '../../app.component';
import { RoleRight } from '../../../kalend';
import { MerchantService } from '../../service/merchant.service';
@Component({
  selector: 'app-merchantwine',
  templateUrl: './merchantwine.component.html',
  styleUrls: ['./merchantwine.component.scss'],
  providers: [MerchantwineService, CommonService, MerchantinfoService, MerchantService]
})
export class MerchantwineComponent extends ComponentBasic implements OnInit {

  loading = true;
  pageIndex = 1;
  pageSize = 15;
  total = 1;
  doc: any = {};
  dataSet = [];
  sdate: any;
  edate: any;
  iscontact: any;
  remarks: any;
  winelist = [];
  name: any;
  // 提现金额
  cashmoney: any;
  isVisible = false;
  isEditVisible = false;
  isOkLoading = false;
  user: any = RoleRight.getStorage();
  LoginUser = RoleRight.getStorage();
  merchantlist = [];
  mid = RoleRight.getStorage().merchantid;
  agent: any;
  mname: any;
  maxApplyCount: any;
  appedCount = 0;
  constructor(private merchservice: MerchantService, private merchantwineService: MerchantwineService,
    private msg: NzMessageService, private commonservice: CommonService) {
    super();
    super.setLayout(true, true);
  }

  ngOnInit() {
    this.merchservice.querybyuserid({
      userid: this.user._id,
      offset: 1,
      limit: 10
    }).then((res) => {
      this.merchantlist = res.rows.map((item) => {
        this.mid = item._id;
        this.mname = item.name;
        this.agent = item.agent.toString();
        return {
          key: item._id,
          value: item.name
        };
      });

      this.searchData();
    })
  }

  searchData(reset: boolean = false): void {
    this.loading = true;
    this.merchantwineService.querybymid({
      mid: this.mid,
      name: this.name,
      offset: this.pageIndex,
      limit: this.pageSize
    }).then((res) => {
      this.loading = false;
      this.total = res.total;
      this.dataSet = res.rows;
    });
  }

  applaySendWine(): void {
    this.merchservice.queryweinmax(this.mid).then((res) => {
      this.maxApplyCount = res.maxnum;
      this.winelist = res.winelist;
      this.appedCount = res.appcount;
      this.isVisible = true;
    });
  }

  handleCancel(): void {
    this.isVisible = false;
  }

  handleOk(): void {
    const wl = [];
    this.winelist.forEach((item) => {
      if (item.count > 0) {
        wl.push({
          wineid: item._id,
          count: item.count * item.specs
        });
      }
    });
    if (this.appedCount > this.maxApplyCount) {
      this.msg.error('申请数量不能大于最大可补货数量');
      return
    } else {
      if (this.appedCount > 0) {
        this.merchantwineService.createWineApply({
          merchantname: this.mname,
          merchantid: this.mid,
          winelist: wl,
          totalcount: this.appedCount,
          AuditStatus: 0,
          SendStatus: 0,
          AgentId: this.agent
        }).then((res) => {
          this.isVisible = false;
          this.msg.info('操作成功,等待管理员审核!');
        });
      } else {
        this.isVisible = false;
        this.msg.info('本次未申请酒饮补货');
      }
    }
  }
}
